// TypeScript file
module Fighter {

    /**
     * AI——郭启航
     * 发送敌机暂时与AiMoney无关
     */
    export class Ai extends egret.DisplayObjectContainer {
        constructor() {
            super(); 
        }
        /**重置Ai难度*/
        public static AiBool: boolean = false;
        /*AI防守数组**/
        public AiDrefense: any[] = [];
        /**ai发送*/
        public AiFiget: any[] = []; 
        /**第一波发兵*/
        public OneTurn() {
            this.AiDrefense = [];
            if (GameUtil.PlayerMessage.instance.game_turns == 1) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                    if (Math.floor(Math.random() * 10) >= 5) {
                        GameUtil.PlayerMessage.instance.AiMoney+=1;
                        GameUtil.PlayerMessage.instance.Aidefense.push(0);
                    }
                }
            }
         GameUtil.PlayerMessage.instance.AiMoney+=GameUtil.PlayerMessage.instance.PlaneNum- GameUtil.PlayerMessage.instance.Aidefense.length
        }
        public AIPlane() {       
            if (!Ai.AiBool) {
                Ai.AiBool = true;
                GameUtil.PlayerMessage.instance.AIdifficulty = Math.random() * 10;
            }
             console.log(GameUtil.PlayerMessage.instance.AIdifficulty);
            this.AiDrefense = [];
            
            for (let i = 0; i < GameUtil.PlayerMessage.instance.PlaneNum; i++) {
                GameUtil.PlayerMessage.instance.AiPlane.push(0);
            }
            //简单Ai
            if (GameUtil.PlayerMessage.instance.AIdifficulty > 0 && GameUtil.PlayerMessage.instance.AIdifficulty < 5) {
                //模拟AI空战
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {        
                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum + 0.5);
                    if (randomNum <= 2) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);
                        GameUtil.PlayerMessage.instance.AiMoney+=1;
                    }
                }
                //Ai防守接受飞机
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }
                //发送特殊敌机
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns; i++) {
                    var AInum = Math.floor(Math.random() * 10 + 0.5);
                    AInum = AInum % 4
                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);
                }
                  GameUtil.PlayerMessage.instance.sendEnemy = [];
            }
            //一般AI
            if (GameUtil.PlayerMessage.instance.AIdifficulty >= 5 && GameUtil.PlayerMessage.instance.AIdifficulty < 8) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum + 0.5);
                    if (randomNum <= 5) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);
                        GameUtil.PlayerMessage.instance.AiMoney+=1;
                    }
                }
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns * 2; i++) {
                    var AInum = Math.random() * 10;
                    AInum = Math.floor(AInum + 0.5);
                    AInum = AInum % 4
                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);
                }
                GameUtil.PlayerMessage.instance.sendEnemy = [];
            }
            //困难AI
            if (GameUtil.PlayerMessage.instance.AIdifficulty >= 8 && GameUtil.PlayerMessage.instance.AIdifficulty <= 10) {
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    //console.log(GameUtil.PlayerMessage.instance.sendEnemy.length);
                    var randomNum = Math.random() * 10;
                    randomNum = Math.floor(randomNum + 0.5);
                    if (randomNum <= 7) {
                        GameUtil.PlayerMessage.instance.sendEnemy.splice(i, 1);
                        GameUtil.PlayerMessage.instance.AiMoney+=1;
                    }
                }
                for (let i = 0; i < GameUtil.PlayerMessage.instance.sendEnemy.length; i++) {
                    GameUtil.PlayerMessage.instance.Aidefense.push(GameUtil.PlayerMessage.instance.sendEnemy[i]);
                }
                for (let i = 0; i < GameUtil.PlayerMessage.instance.game_turns * 3; i++) {
                    var AInum = Math.random() * 10;
                    AInum = Math.floor(AInum + 0.5);
                    AInum = AInum % 4
                    GameUtil.PlayerMessage.instance.AiPlane.push(AInum);
                }
                GameUtil.PlayerMessage.instance.sendEnemy = [];
            }
        }
    }

}